
#include "hal_pwm.h"

static PWM_Config_MCB config_mcb =
{
		PWM_INTERNAL_COUNTER ,
		PWM_UC_PRESCALE_DIV1 ,
		MCB_COUNTER_UP_MODE ,
		100,
		10
};

static PWMConfig_t pwm_config = {
	PWM_MCB_MODE ,
	NULL ,
	&config_mcb ,
	NULL ,
	NULL 
};

static PWM_Config_OPWMB config_opwmb ={
		PWM_COUNTER_BUS_A ,
		PWM_UC_PRESCALE_DIV1 ,
		START_RISING_EDGE ,
		0,
		0
};

static PWMConfig_t pwm_config_ch = {
	PWM_OPWMB_MODE ,
	NULL ,
	NULL ,
	&config_opwmb ,
	NULL 
};

void hal_pwm_init(pwm_channel_en channel , uint32_t hz , uint16_t duty , enabled_en en)
{
	static uint8_t init_flag0 = 1;
	static uint8_t init_flag1 = 1;	
	
	if((channel <= PWM0_CHANNEL31)&&(channel >= PWM0_CHANNEL0))
	{
		if(1 == init_flag0)
		{
			pwm_config.config_mcb->up_mode_peroid_value = (uint16_t)(1000000/hz);
			PWM_LLD_Init(&PWMD_0,&EMIOS_0 , HAL_SYS_CLOCK_MHZ);
			PWM_LLD_UC_Channel_Init(&PWMD_0, 23, &pwm_config);
			PWM_LLD_UC_Channel_Enable(&PWMD_0, 23);
			init_flag0 = 0;
		}
		else
		{
			
		}
		hal_pwm_duty(channel , duty);
		hal_pwm_enabled(channel , en);
	}
	else
	{
		
	}
	
	if((channel <= PWM1_CHANNEL31)&&(channel >= PWM1_CHANNEL0))
	{
		if(1 == init_flag1)
		{
			pwm_config.config_mcb->up_mode_peroid_value = (uint16_t)(1000000/hz);
			PWM_LLD_Init(&PWMD_1,&EMIOS_1 , HAL_SYS_CLOCK_MHZ);
			PWM_LLD_UC_Channel_Init(&PWMD_1, 23, &pwm_config);
			PWM_LLD_UC_Channel_Enable(&PWMD_1, 23);
			init_flag1= 0;
		}
		else
		{
			
		}
		hal_pwm_duty(channel , duty);
		hal_pwm_enabled(channel , en);
	}
	else
	{
	
	}
}

void hal_pwm_enabled(pwm_channel_en channel , enabled_en en)
{
	if((PWM0_CHANNEL23 == channel)||(PWM1_CHANNEL23 == channel))
	{
		
	}
	else
	{
		if((PWM0_CHANNEL31 >= channel) &&(PWM0_CHANNEL0 <= channel))
		{
			if(HAL_DISABLE == en)
			{
				PWM_LLD_UC_Channel_Disable(&PWMD_0 ,channel);
			}
			else if(HAL_ENABLE == en)
			{
				PWM_LLD_UC_Channel_Enable(&PWMD_0 ,channel);
			}
			else
			{
				
			}
		}
		else if((PWM1_CHANNEL31 >= channel)&&( PWM1_CHANNEL0 <= channel))
		{
			if(HAL_DISABLE == en)
			{
				PWM_LLD_UC_Channel_Disable(&PWMD_1 ,(channel - PWM1_CHANNEL0));
			}
			else if(HAL_ENABLE == en)
			{
				PWM_LLD_UC_Channel_Enable(&PWMD_1 ,(channel - PWM1_CHANNEL0));
			}
			else
			{
				
			}
		}
		else if((ICU0_CHANNEL0 <= channel) && (ICU0_CHANNEL31 >= channel))
		{
			if(HAL_DISABLE == en)
			{
				
				
			}else if(HAL_ENABLE == en)
			{
				
			}
		}
		else if((ICU1_CHANNEL0 <= channel) &&(ICU1_CHANNEL31 >= channel))
		{
			if(HAL_DISABLE == en)
			{
				
			}else if(HAL_ENABLE == en)
			{
				
			}
		}
		else
		{
			
		}
	}
}
void hal_pwm_duty(pwm_channel_en channel , uint16_t duty)
{
	if((PWM0_CHANNEL23 == channel)||(PWM1_CHANNEL23 == channel))
	{
		
	}else
	{
		if(PWM0_CHANNEL31 >= channel)
		{
			pwm_config_ch.config_opwmb->b = (pwm_config.config_mcb->up_mode_peroid_value * duty / 100 );
			PWM_LLD_UC_Channel_Init(&PWMD_0 , channel , &pwm_config_ch);
			
		}
		else if(PWM1_CHANNEL31 >= channel)
		{
			pwm_config_ch.config_opwmb->b = (pwm_config.config_mcb->up_mode_peroid_value * duty / 100 );
			PWM_LLD_UC_Channel_Init(&PWMD_1 , (channel-PWM1_CHANNEL0) , &pwm_config_ch);
		}
		else
		{
			
		}
	}
}

static void icuedgecount(const ICUDriver *icudriver)
{
	icudriver = icudriver;
}

static void icuwidthcbk(const ICUDriver *icudriver)
{
	icudriver = icudriver;
}

static void icuperiodcbk(const ICUDriver *icudriver)
{
	icudriver = icudriver;
}


static ICUConfig_t config_icu;
void hal_capture_init(pwm_channel_en channel ,enabled_en en)
{	
	static uint8_t init_flag2 = 1;
	static uint8_t init_flag3 = 1;
//	ICUConfig_t config_icu = {0};
	cc_memset((uint8_t *)&config_icu, 0, sizeof(config_icu));
	config_icu.hw_mode = ICU_INPUT_SINGLE_ACTION_MODE;
	config_icu.config_saic.usermode = ICU_USER_MODE_WIDTH_PERIOD_MEASUREMENT;
	config_icu.config_saic.clock_bus = ICU_INTERNAL_COUNTER;
	config_icu.config_saic.uc_prescale = ICU_UC_PRESCALE_DIV1;
	config_icu.config_saic.edge_polarity = BOTH_EDGE;
	config_icu.config_saic.edge_count_cb = icuedgecount;
	config_icu.config_saic.width_cb = icuwidthcbk;
	config_icu.config_saic.period_cb = icuperiodcbk;
	
	if((channel >= ICU0_CHANNEL0)&&(channel <= ICU0_CHANNEL31))
	{
		if(1 == init_flag2)
		{
			ICU_LLD_Init(&ICUD_0 , &EMIOS_0 , HAL_SYS_CLOCK_MHZ);	
			init_flag2 = 0;
		}
		else
		{
			
		}
		ICU_LLD_UC_Channel_Init(&ICUD_0 , (channel - ICU0_CHANNEL0) , &config_icu);
		ICU_LLD_Interrupt_Register(&ICUD_0, (channel - ICU0_CHANNEL0));
		hal_capture_enabled(channel , en);
	}
	else
	{
		
	}
	if((channel >= ICU1_CHANNEL0)&&(channel <= ICU1_CHANNEL31))
	{
		if(1 == init_flag3)
		{
			ICU_LLD_Init(&ICUD_1 , &EMIOS_1 , HAL_SYS_CLOCK_MHZ);
			init_flag3 = 0;
		}
		else
		{
			
		}
		ICU_LLD_UC_Channel_Init(&ICUD_1 , (channel - ICU1_CHANNEL0) , &config_icu);
		ICU_LLD_Interrupt_Register(&ICUD_1, (channel - ICU1_CHANNEL0));
		hal_capture_enabled(channel , en);
	}
	else
	{
		
	}
}

void hal_capture_enabled(pwm_channel_en channle , enabled_en en)
{
	if((channle >= ICU0_CHANNEL0)&&(channle <= ICU0_CHANNEL31))
	{
		if(HAL_ENABLE == en)
		{
			ICU_LLD_UC_Channel_Enable(&ICUD_0 , (channle - ICU0_CHANNEL0));
		}
		else if(HAL_DISABLE == en)
		{
			ICU_LLD_UC_Channel_Disable(&ICUD_0 , (channle - ICU0_CHANNEL0));
		}
		else 
		{
			
		}
	}
	else if((channle >= ICU1_CHANNEL0)&&(channle <= ICU1_CHANNEL31))
	{
		if(HAL_ENABLE == en)
		{
			ICU_LLD_UC_Channel_Enable(&ICUD_1 , (channle - ICU1_CHANNEL0));
		}
		else if(HAL_DISABLE == en)
		{
			ICU_LLD_UC_Channel_Disable(&ICUD_1 ,(channle - ICU1_CHANNEL0));
		}
		else
		{
			
		}
	}
	else
	{
		
	}
}


uint32_t hal_pwm_get_period(pwm_channel_en channel)
{
	uint32_t period = 0;
	if((channel >= ICU0_CHANNEL0)&&(channel <= ICU0_CHANNEL31))
	{
		period = ICUD_0.data.saic_data.period[channel-ICU0_CHANNEL0];
	}
	else if((channel >= ICU1_CHANNEL0)&&(channel <= ICU1_CHANNEL31))
	{
		period = ICUD_1.data.saic_data.period[channel-ICU0_CHANNEL0];
	}
	else
	{
		period = 0;
	}
	return  period;
}
uint16_t hal_pwm_get_duty(pwm_channel_en channel)
{
	uint32_t duty = 0;
	if((channel >= ICU0_CHANNEL0)&&(channel <= ICU0_CHANNEL31))
	{
		if(ICUD_0.data.saic_data.period[channel-ICU0_CHANNEL0] != 0)
		{
			duty =100 - 100 * ICUD_0.data.saic_data.width[channel-ICU0_CHANNEL0] / ICUD_0.data.saic_data.period[channel-ICU0_CHANNEL0];
		}
		else
		{
			duty = 0;
		}
	}
	else if((channel >= ICU1_CHANNEL0)&&(channel <= ICU1_CHANNEL31))
	{
		if(ICUD_1.data.saic_data.period[channel-ICU1_CHANNEL0] != 0)
		{
			duty =100 - 100 * ICUD_1.data.saic_data.width[channel-ICU1_CHANNEL0]/ ICUD_1.data.saic_data.period[channel-ICU1_CHANNEL0];
		}
		else
		{
			duty = 0;
		}
	}
	else
	{
		duty = 0;
	}
	return  duty;
}

void hal_pwm_peroid(pwm_channel_en channel , uint32_t peroid)
{
	
}

result_en hal_pwm_clock(pwm_channel_en channel ,uint32_t hz)
{
	
}



